package org.penguin.study.bk.util

import java.util.*

object LanguageUtil {

    val allAvailableLocales = listOf(Locale.getAvailableLocales())

    val supportedLocales = listOf(
        Locale.forLanguageTag("zh-CN"),
        Locale.forLanguageTag("zh-HK"),
        Locale.forLanguageTag("zh-TW"),
        Locale.forLanguageTag("en-US"),
        Locale.forLanguageTag("de-DE"),
        Locale.forLanguageTag("fr-FR"),
        Locale.forLanguageTag("es-ES"),
        Locale.forLanguageTag("it-IT"),
        Locale.forLanguageTag("ja-JP"),
        Locale.forLanguageTag("en-*"),
    )

    fun findBestMatch(
        language: String,
        supported: List<Locale>
    ): Locale {
        return Locale.lookup(Locale.LanguageRange.parse(language), supported)
    }
    fun toLanguageRanges(locales: List<Locale>): List<Locale.LanguageRange> {
        val languageRanges = ArrayList<Locale.LanguageRange>()
        for (locale in locales) {
            languageRanges.add(toLanguageRange(locale))
        }
        return languageRanges
    }

    private fun toLanguageRange(locale: Locale): Locale.LanguageRange {
        return Locale.LanguageRange(locale.toLanguageTag())
    }
}